
import {createStore, applyMiddleware,combineReducers,compose} from 'redux';
import thunk from 'redux-thunk'
//
const  CAUSED_ADD="CAUSED_ADD";
const  CAUSED_DETELE="CAUSED_DETELE";
const  CAUSED_EDIT="CAUSED_EDIT";
const  CAUSED_AFTER_ADD="CAUSED_AFTER_ADD";
const  CAUSED_CLEAR="CAUSED_CLEAR";

function Caused(state=[],action){
  switch(action.type){
      case CAUSED_AFTER_ADD:
      let AfterNewState=state;
      AfterNewState.unshift({});
      return AfterNewState;
     
      case CAUSED_ADD:
       let newState=state;
       newState[action.index]=action.data;
      return newState;
      case CAUSED_DETELE:
       let spState=state;
        spState.splice(action.index,1)
       return spState ;
       case CAUSED_CLEAR:
       return []
       default:
      return state
  }
}

// export  function SetCausedAsync(user){
//     console.log("reduxuser",user);
//     return async dispatch=>{
//         try {
//             await AsyncStorage.setItem("user",JSON.stringify(user));
//             dispatch(SetUser(user))
//             console.log("成功");
//           } catch (error) {
//             console.log("失败");
//             dispatch(SetUser(defaultUser))
//           }
//     }
// }
// export  function GetCausedAsync(){
    
//     return async dispatch=>{
//         try {
//             let user = await AsyncStorage.getItem('user');
//             user=JSON.parse(user);
//             if (user&&user!==null&&user!=''&&user.userId!=""){
//                dispatch(GetUser(user))
             
//             }else{
//                 dispatch(GetUser(defaultUser))
//             }
//             } catch (error) {
//              dispatch(GetUser(defaultUser))
//           }
//     }
// }

export function causedAfterAdd(){
    return{
        type:CAUSED_AFTER_ADD,
     }
 }
export function causedAdd(index,data){
   return{
       type:CAUSED_ADD,
       index,
       data
   }
}

export function causedDetele(index){
    return{
        type:CAUSED_DETELE,
        index,
     }
 }
export function causedClear(){
    return{
        type:CAUSED_CLEAR
      
     }
 }
/**
 * 用户
 */
const USER_INFO_GET="USER_INFO_GET";
const USER_INFO_SET="USER_INFO_SET";
const USER_INFO_LOGOUT="USER_INFO_LOGOUT";
const USER_LOGIN="USER_LOGIN";

let defaultUser= {
  		"id": "",
        "userName": "",
        "password":"",
	    "userPwd":"",
		"userType": "",
		"isType": "",
		"userDeptId": "",
        "powerGroup": [],
        "arrList":[],
        "selectIndex":-1
};

 function User(state=defaultUser,action){
    switch(action.type){
        case USER_INFO_SET:
        return Object.assign({}, action.user);;
        case USER_INFO_GET:
        return Object.assign({}, action.user);;
        case USER_INFO_LOGOUT:
        return defaultUser;
        //  case USER_LOGIN：
              
        //  return;
         default:
         return state
    }
}


export function GetUser(user){
    return{
        type:USER_INFO_GET,
        user
    }
}
export function SetUser(user){
    return{
        type:USER_INFO_SET,
        user
    }
}
export function Login(){
    return{
        type:USER_LOGIN
    }
}

export  function SetUserAsync(user){
    console.log("reduxuser",user);
    // return async dispatch=>{
    //     try {
    //         await AsyncStorage.setItem("user",JSON.stringify(user));
    //         dispatch(SetUser(user))
    //         console.log("成功");
    //       } catch (error) {
    //         console.log("失败");
    //         dispatch(SetUser(defaultUser))
    //       }
    // }
}
export  function GetUserAsync(callback){
    
    // return async dispatch=>{
    //     try {
    //         let user = await AsyncStorage.getItem('user');
    //         user=JSON.parse(user);
    //         if (user&&user!==null&&user!=''&&user.userId!=""){
    //            dispatch(GetUser(user))
             
    //         }else{
    //             dispatch(GetUser(defaultUser))
             
    //         }
    //         callback(true)
    //         } catch (error) {
    //          dispatch(GetUser(defaultUser))
    //          callback(false)
    //       }
    // }
}

export function LogOut(){
    
    return  dispatch=>{
       dispatch(SetUser(defaultUser))
    };
 }
export function LogOutUser(){
    
   return async dispatch=>{
      dispatch(SetUserAsync(defaultUser))
   };
}
const rootRedux = combineReducers({
    user:User,
    caused:Caused
})
//合并reducer
export const store = createStore(rootRedux,compose(applyMiddleware(thunk)));
